package com.crm.redis.service.customerManager.impl;

import com.crm.model.entity.customerManager.Customer;
import com.crm.model.entity.sys.UserAccount;
import com.crm.redis.RedisPrefix;
import com.crm.redis.mapper.ValueRMapper;
import com.crm.redis.service.customerManager.CustomerRedisService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:chaifuyou
 * @Date: 2021/4/15 9:14
 */
@Service
public class CustomerRedisServiceImpl implements CustomerRedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private String getAnalyseCustomerKey(String accountId) {
        String key = RedisPrefix.ANALYSE_CUSTOMER + accountId;
        return key;
    }

    @Override
    public void addAnalyseCustomerToRedis(String accountId,String jsonDate) {
        /*if (null == jsonDate){
            jsonDate = "{intentionCustomerCount:23,budgetCustomerCount:2,followCustomerCount:33,rivalCustomerCount:1,outCustomerCount:0}";
        }*/
        jsonDate = "{intentionCustomerCount:23,budgetCustomerCount:2,followCustomerCount:33,rivalCustomerCount:1,outCustomerCount:0}";
        String key =getAnalyseCustomerKey(accountId);
        boolean result = ValueRMapper.setRedisString(redisTemplate, key,jsonDate);
    }

    /**
     * 客户意向分析
     * @param accountId
     */
    @Override
    public String getAnalyseCustomerIntention(String accountId) {
        String key =getAnalyseCustomerKey(accountId);
        String jsonDate = ValueRMapper.getRedisString(redisTemplate,key);
        return jsonDate;
    }

    /**
     * redis里的跟进标签
     * @return
     */
    @Override
    public String selectFollowTag() {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        String key = RedisPrefix.FOLLOW_TAG_COUNT + userAccount.getAccountId();
        String jsonDate = ValueRMapper.getRedisString(redisTemplate,key);
        return jsonDate;
    }

    /**
     * 添加跟进标签
     * @param tabStr
     */
    @Override
    public void addFollowTagsToRedis(String tabStr) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        String key = RedisPrefix.FOLLOW_TAG_COUNT + userAccount.getAccountId();
        ValueRMapper.setRedisString(redisTemplate,key,tabStr);
    }

    /**
     * 判断客户名称是否存在
     * @Author pym
     * @Date  2021/7/1 14:24
     * @param customerName 客户名称
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean isCustomerExist(String customerName) {
        String key = RedisPrefix.CUSTOMER_LIBRARY + customerName;
        return ValueRMapper.hasKey(redisTemplate,key);
    }

    /**
     * 根据客户名称去Redis查统一社会信用代码
     * @Author pym
     * @Date  2021/7/1 14:48
     * @param customerName 客户名称
     * @return {@link {java.lang.String}}
     */
    @Override
    public String selectUnifiedSocialCreditCode(String customerName) {
        String key = RedisPrefix.CUSTOMER_LIBRARY + customerName;
        return ValueRMapper.getRedisString(redisTemplate,key);
    }

    /**
     * 获取会员分析
     * @param companyId
     * @return
     */
    @Override
    public String getVipCustomerAnalyseByCompany(String companyId) {
        String key = RedisPrefix.VIP_CUSTOMER_ANALYSE + companyId;
        return ValueRMapper.getRedisString(redisTemplate,key);
    }

    /**
     * 获取会员流失
     * @param companyId
     * @param year
     * @return
     */
    @Override
    public String getOutVipAgainBuyRatio(String companyId, Integer year) {
        String key = RedisPrefix.OUT_VIP_CUSTOMER_ANALYSE + companyId+":"+year;
        return ValueRMapper.getRedisString(redisTemplate,key);
    }

    /**
     * 获取会员交易
     * @param companyId
     * @param year
     * @return
     */
    @Override
    public String getVipCustomerBuyRise(String companyId, Integer year) {
        String key = RedisPrefix.VIP_CUSTOMER_BUT_RISE + companyId+":"+year;
        return ValueRMapper.getRedisString(redisTemplate,key);
    }

    /**
     * 获取会员数量
     * @param year
     * @param companyId
     * @return
     */
    @Override
    public String getVipCustomerCount(Integer year, String companyId) {
        String key = RedisPrefix.VIP_CUSTOMER_COUNT + companyId+":"+year;
        return ValueRMapper.getRedisString(redisTemplate,key);
    }

    /**
     * 客户消费总额度
     * @param year
     * @param companyId
     * @return
     */
    @Override
    public String getCustomerConsumeCount(Integer year, String companyId) {
        String key = RedisPrefix.CUSTOMER_CONSUME_COUNT + companyId+":"+year;
        return ValueRMapper.getRedisString(redisTemplate,key);
    }

    /**
     * 续费会员数量
     * @param year
     * @param companyId
     * @return
     */
    @Override
    public String getRenewVipCustomerCount(Integer year, String companyId) {
        String key = RedisPrefix.RENEW_VIP_CUSTOMER_COUNT + companyId+":"+year;
        return ValueRMapper.getRedisString(redisTemplate,key);
    }

    /**
     * 往Redis里添加客户信息
     * @Author pym
     * @Date  2021/7/21 13:48
     * @param list 客户集合
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean addCustomerToRedis(List<Customer> list) {
        boolean result = false;
        for(Customer customer : list) {
            String key = RedisPrefix.CUSTOMER_IS_REPEAT + customer.getCustomerName() + "_" + customer.getUnifiedSocialCreditCode();
            result = ValueRMapper.setRedisObject(redisTemplate, key,customer);
        }
        return result;
    }

    /**
     * 通过客户名称和统一社会信用代码查询客户
     * @Author pym
     * @Date  2021/8/3 14:43
     * @param customerName 客户名称
     * @param unifiedSocialCreditCode 统一社会信用代码
     * @return {@link {com.crm.model.entity.customerManager.Customer}}
     */
    @Override
    public Customer getCustomerByNameAndCode(String customerName, String unifiedSocialCreditCode) {
        String key = RedisPrefix.CUSTOMER_IS_REPEAT + customerName + "_" + unifiedSocialCreditCode;
        Customer customer =  ValueRMapper.getRedisObject(redisTemplate,key, Customer.class);
        return customer;
    }

    /**
     * 将客户信息插入Redis
     * @Author pym
     * @Date  2021/8/3 14:51
     * @param customer 客户信息
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean setCustomerByNameAndCode(Customer customer) {
        String key = RedisPrefix.CUSTOMER_IS_REPEAT + customer.getCustomerName() + "_" + customer.getUnifiedSocialCreditCode();
        return ValueRMapper.setRedisObject(redisTemplate,key,customer);
    }
}
